package com.hotdog.service;



import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;



import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hotdog.mapper.TbWatchLogsMapper;
import com.hotdog.mapper.ViewAddrMapper;
import com.hotdog.pojo.IndexInfo;

import com.hotdog.pojo.TbWatchLogs;
import com.hotdog.vo.PageResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.Collection;
import java.util.HashMap;
import java.util.List;

/**
 * 获取主页信息的实现类
 * @author 郭鲁政
 */
@Slf4j
@Service
public class ViewAddrServiceImpl implements ViewAddrService {
    @Autowired
    private ViewAddrMapper viewAddrMapper;
    @Autowired
    private TbWatchLogsMapper tbWatchLogsMapper;


    @Override
    /**
     * 分页查询
     */
    public PageResult getAll(PageResult pageResult) {
        System.out.println(SecurityContextHolder.getContext().getAuthentication()+"********************");
        SecurityContextHolder.getContext().getAuthentication();
        Collection<? extends GrantedAuthority> authorities = SecurityContextHolder.getContext().getAuthentication().getAuthorities();//获取当前用户权限
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(authorities);
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println();
        System.out.println(SecurityContextHolder.getContext().getAuthentication().getCredentials());
        SecurityContextHolder.getContext().getAuthentication().getDetails();
        Page<IndexInfo> pageSearchPage = new Page<>(pageResult.getPageNum(), pageResult.getPageSize());
        boolean b = StringUtils.hasLength(pageResult.getQuery());
        QueryWrapper<IndexInfo> title = new QueryWrapper<IndexInfo>().like(b, "title", pageResult.getQuery());
        IPage<IndexInfo> indexInfoPage = viewAddrMapper.selectPage(pageSearchPage, title);
        List<IndexInfo> records = indexInfoPage.getRecords();
        long total=viewAddrMapper.selectCount(null);
        pageResult.setTotal(total).setRows(records);
        return pageResult;
    }

    @Override
    public PageResult getHistory(PageResult pageResult) {
        String name = SecurityContextHolder.getContext().getAuthentication().getName();
        int total = tbWatchLogsMapper.getUserName(name);
        log.info(name);
        List<IndexInfo> getinfo = tbWatchLogsMapper.getinfo(name, pageResult.getPageNum()-1, pageResult.getPageSize());
log.info(getinfo.toString());
        pageResult.setTotal((long) total).setRows(getinfo);
        return pageResult;

    }
}
